package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import model.Notice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import dao.NoticeDao;
import dao.RoleDao;

@Service
public class NoticeService {
	@Autowired
	private NoticeDao noticeDao;
	@Autowired
	private RoleDao roleDao;

	public void saveOrUpdate(Notice notice) {
		notice.setLasttime(new Date());
		if (notice.getStatus().equals(2)) {
			notice.setPublishtime(new Date());
		}
		if (notice.getId() == null) {
			noticeDao.save(notice);
		} else {
			noticeDao.update(notice);
		}
	}

	public void delete(String ids) {
		String[] idStrs = ids.split(",");
		for (String id : idStrs) {
			Integer _id = Integer.parseInt(id);
			// 删除用户
			noticeDao.delete(_id);
		}

	}

	public void publish(String ids) {
		String[] idStrs = ids.split(",");
		Notice notice = new Notice();
		for (String id : idStrs) {
			Integer _id = Integer.parseInt(id);
			notice = noticeDao.getById(_id);
			notice.setPublishtime(new Date());
			notice.setStatus(2);
			noticeDao.update(notice);
		}

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageList<HashMap<String, Object>> queryPage(int page, int limit,
			HashMap<String, Object> map) {
		Integer userid = (Integer) map.get("temp");
		List<HashMap<String, Object>> roles = roleDao.getRoles(userid);
		int roleFlag = 4; // 初始最低权限
		for (int i = 0; i < roles.size(); i++) {
			Integer roleid = (Integer) roles.get(i).get("roleid");
			/* 最高权限:开发、系统 业务管理、公告管理员 */
			if (roleid.equals(1) || roleid.equals(7) || roleid.equals(8)
					|| roleid.equals(14)) {
				roleFlag = 1;
				break; // 最高权限，跳出循环
			}
			/* 市质监局 */
			else if (roleid.equals(9)) {
				roleFlag = 2;
			}
			/* 区质监局 */
			else if (roleid.equals(10)) {
				roleFlag = 3;
			}
			/* 社区管理员以及平民 */
			else {
				roleFlag = 4;
			}
		}
		PageList<HashMap<String, Object>> pagelist = new PageList<HashMap<String, Object>>();
		PageBounds pageBounds = new PageBounds(page, limit);
		ArrayList al = new ArrayList();
		if (roleFlag == 1 || roleFlag == 2) {
			al.add(1);
			al.add(2);// 状态为1，2
			map.put("status", al);
			pagelist = (PageList<HashMap<String, Object>>) noticeDao.queryPage(
					pageBounds, map);
		} else {
			al.add(2); // 状态为2（已发布状态）
			map.put("status", al);
			pagelist = (PageList<HashMap<String, Object>>) noticeDao.queryPage(
					pageBounds, map);
		}
		return pagelist;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageList<HashMap<String, Object>> queryPage4All(int page, int limit,
			HashMap<String, Object> map) {
		PageList<HashMap<String, Object>> pagelist = new PageList<HashMap<String, Object>>();
		PageBounds pageBounds = new PageBounds(page, limit);
		ArrayList al = new ArrayList();
		al.add(2); // 状态为2（已发布状态）
		map.put("status", al);
		pagelist = (PageList<HashMap<String, Object>>) noticeDao.queryPage4All(
				pageBounds, map);
		return pagelist;
	}

	public Notice getById(Integer id) {
		return noticeDao.getById(id);
	}
	
	public HashMap<String, Object> queryNotice(Integer id){
		return noticeDao.queryNotice(id);
	}
}
